import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomi_shop/app/services/searchService.dart';

class SearchController extends GetxController {
  TextEditingController textEditingController = TextEditingController();
  final guestLikeList = ["手机", "电脑", "洗衣机", "空调", "电视机", "路由器", "冰箱"];
  var searchHistoryList = [].obs;
  String keyword = "";
  @override
  void onInit() {
    super.onInit();
    getSearchHistory();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  getSearchHistory() async {
    List? history = await SearchService.getSearchHistory();
    if (history != null) {
      searchHistoryList.value = history;
    }
  }

  addSearchHistory() async {
    if (keyword.isNotEmpty) {
      await SearchService.addSearchHistory(keyword);
      await getSearchHistory();
    }
  }

  cleanSearchHistory() async {
    await SearchService.cleanSearchHistory();
    await getSearchHistory();
  }

  deleteSearchHistory() async {
    if (keyword.isNotEmpty) {
      await SearchService.deleteSearchHistory(keyword);
      await getSearchHistory();
    }
  }
}
